package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.mapper.NodeMapper;
import com.dkd.manage.mapper.VmTypeMapper;
import com.dkd.manage.service.IChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;

/**
 * 设备管理Service业务层处理
 *
 * @author lql
 * @date 2024-09-11
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService {
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private NodeMapper nodeMapper;

    @Autowired
    private VmTypeMapper vmTypeMapper;

    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询设备管理
     *
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id) {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     *
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine) {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     *
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int insertVendingMachine(VendingMachine vendingMachine) {
        //1.添加设备信息
        //1-1设置设备编号
        String innerCode = UUIDUtils.getUUID();
        vendingMachine.setInnerCode(innerCode);
        //1-2查询设备类型表，补充设备容量
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        //1-3设置商圈类型、区域ID、合作商ID
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId()); //获取设备所属点位表的信息，其中包含商圈类型、区域ID、合作商ID和详细地址
        BeanUtil.copyProperties(node, vendingMachine, "id");
        //1-4设置详细地址
        vendingMachine.setAddr(node.getAddress());
        //1-5设置设备状态
        vendingMachine.setVmStatus(0L); // 0 表示未投放
        //1-6设置创建时间和修改时间
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setUpdateTime(vendingMachine.getCreateTime());
        //1-7保存
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);

        //2.添加货道信息
        //2-1存储货道集合
        List<Channel> channelList = new ArrayList<>();
        //2-2双重for循环，录入货道信息
        for (int i = 1; i <= vmType.getVmRow(); i++) { //行
            for (int j = 1; j <= vmType.getVmCol(); j++) { //列
                Channel channel = new Channel();
                channel.setVmId(vendingMachine.getId()); //货道机编号
                channel.setInnerCode(innerCode); //销售机编号
                channel.setMaxCapacity(vmType.getChannelMaxCapacity()); //货道最大容量
                channel.setChannelCode(i + "-" + j); //货道编号
                channel.setCreateTime(DateUtils.getNowDate()); //创建时间
                channel.setUpdateTime(channel.getCreateTime()); //修改时间
                channelList.add(channel);
            }
        }
        //2-3批量保存货道信息
        channelMapper.batchInsertChannels(channelList);
        return result;
    }

    /**
     * 修改设备管理
     *
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine) {
        if(vendingMachine.getNodeId() != null){
            //查询点位表，设置商圈类型、区域ID、合作商ID
            Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId()); //获取设备所属点位表的信息，其中包含商圈类型、区域ID、合作商ID和详细地址
            BeanUtil.copyProperties(node, vendingMachine, "id");
            //设置详细地址
            vendingMachine.setAddr(node.getAddress());
        }
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     *
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids) {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     *
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id) {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    /**
     * 根据售货机编号查询设备信息
     * @param innerCode
     * @return VendingMachine
     */
    @Override
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {
        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }
}
